package com.server.core.handle;

import com.server.core.action.AbstractAction;
import com.server.core.domain.Request;
import com.server.core.domain.Response;
import com.server.core.util.ActionUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;


public class HttpServerHandler extends  SimpleChannelInboundHandler<FullHttpRequest> {
        private WebSocketServerHandshaker handshaker;

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) throws Exception {
                String uri = fullHttpRequest.uri();

                if(!uri.equals("/websocket")) {
                   Request request  = new Request(ctx, fullHttpRequest);
                   Response response = new Response(ctx);
                   response.send(ctx, request);
                   System.out.println(request.getUri());
                   return;
                }

                // websocket sharkhand
                WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                        "ws://localhost:8080/websocket", null, false);
                handshaker = wsFactory.newHandshaker(fullHttpRequest);
                 if (handshaker == null) {
                        WebSocketServerHandshakerFactory
                                .sendUnsupportedVersionResponse(ctx.channel());
                } else {
                        handshaker.handshake(ctx.channel(), fullHttpRequest);
                }
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {

                super.channelActive(ctx);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {

                super.channelInactive(ctx);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                super.exceptionCaught(ctx, cause);
                cause.printStackTrace();
        }

        /**
         * 执行action
         * @param request
         * @param response
         */
        public void doAction(Request request, Response response)throws Exception {
            System.out.println(request.getUri());
            AbstractAction abstractAction = ActionUtil.getAction(request.getMethod().toString(),request.getUri());
        }



       public void shakeHand(ChannelHandlerContext ctx, FullHttpRequest req) {
        // 构造握手响应返回，本机测试
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://localhost:8080/websocket", null, false);
        handshaker = wsFactory.newHandshaker(req);
        if (handshaker == null) {
                WebSocketServerHandshakerFactory
                        .sendUnsupportedVersionResponse(ctx.channel());
        } else {
                handshaker.handshake(ctx.channel(), req);
        }
       }

}
